import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


const store = new Vuex.Store({
  state: {
    count: 100,
    toTos: [
      {
        title: "thing1",
        finished: true
      }, {
        title: "thing2",
        finished: true
      }
    ]
  },getters:{
      finished:function (state) {
            return state.toTos.filter(function (item) {
              return item.finished;
            })
      },
    summary:function (state) {
         return {
             finished:state.toTos.filter(function (item) {
                 return item.finished
             }).length,
           unfinished:state.toTos.filter(function (item) {
             return item.finished==false
           }).length
         }
    }
  },mutations:{
     increment:function (state) {
           return ++state.count;
     },
     plus:function (state,num1) {

         return  state.count+=num1;

     },

    plus2:function (state,payload) {

      return  state.count+=parseInt(payload.num1);

    }
  },actions: {
    increment (context) {
      context.commit('increment')
    },
    incrementAsync ({ commit }) {
      setTimeout(() => {
        commit('increment')
      }, 1000)
    }
  }
})


export default store
